package com.csyf.manager.controller.api;

import com.csyf.manager.entity.SysPayee;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysPayeeBiz;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.ReturnData;
import com.csyf.manager.utils.page.JqGridVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: casper
 * @Description: 常用联系人接口
 * @Date: Created in 2018/9/17 10:00
 */
@Controller
@RequestMapping(value = "boss/payee")
public class ApiPayeeController {

    @Autowired
    private SysPayeeBiz sysPayeeBiz;

    @Autowired
    private HttpSession httpSession;

    @GetMapping(value = "view/{pageName}")
    public String viewPage(@PathVariable(value = "pageName") String pagename) {
        return "boss/" + pagename;
    }

    /**
     * 查询所有的常用出款人信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryAllPayee")
    public ReturnData queryAllPayee() {
        ReturnData returnData = new ReturnData();

        List<SysPayee> sysPayees = sysPayeeBiz.queryAll();
        returnData.setCode(0);
        returnData.setMsg("查询成功");
        returnData.setData(sysPayees);
        return returnData;
    }

    /**
     * 查询常用出款人列表
     * @param vo
     * @param rows
     * @param page
     * @return
     */
    @ResponseBody
    @PostMapping(value = "payeeDataList")
    public JqGridVo payeeDataList(SysPayee vo, Integer rows, Integer page) {
        return sysPayeeBiz.querydata(vo, page, rows);
    }

    /**
     * 根据id查询出款人信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryPayeeById", method = RequestMethod.GET)
    public ResultEntity queryPayeeById(@RequestParam(value = "payeeId") String payeeId) {
        SysPayee sysPayee = sysPayeeBiz.queryPayeeById(payeeId);
        return ResultUtli.DEFAULT_SUCCESS("成功", sysPayee);
    }

    /**
     * 保存/修改出款人信息
     * @param vo
     * @return
     */
    @ResponseBody
    @PostMapping(value = "operPayee")
    public ResultEntity operPayee(SysPayee vo) {
        try {
            //添加
            if (null == vo.getId()) {
                sysPayeeBiz.savePayee(vo);
                return ResultUtli.DEFAULT_SUCCESS("保存成功", null);
            } else {
                //修改
                int i = sysPayeeBiz.updatePayeeById(vo);
                if (i > 0) {
                    return ResultUtli.DEFAULT_SUCCESS("修改成功", null);
                } else {
                    return ResultUtli.ERRO(10, "修改失败，请联系技术人员", null);
                }
            }

        } catch (ApiValidateException e) {
            e.printStackTrace();
            return ResultUtli.ERRO(10, "操作失败，请联系技术人员", null);
        }
    }


    /**
     * 删除出款人信息
     * @param payeeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deletePayee")
    public ResultEntity deletePayee(Integer payeeId) {
        try {
            sysPayeeBiz.deletePayeeById(payeeId);
            return ResultUtli.DEFAULT_SUCCESS("删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtli.ERRO(10, "删除失败，请联系技术人员", null);
        }

    }

}
